<?php
namespace Api
{
/**
 * global template variables
 * @author Łukasz Torba 2013  
 */
 class  Config
 {
    const _CONFIG_ = 'API';
    
    /**
     * Called object parameters address
     * @var array
     */
    static public $param = array();
     
    /**
     * Current controller
     * @var reference Controller()
     */
    static public $ctrl = null;
           
    /**
     * Current view
     * @var reference Content
     */
    static public $view = null;
     
    /**
     * Object instance flag
     */   
    static private $instance = null;
    
    /**
     * Config privilages
     */
    static private $privilages = null;
    
    /**
     * Debug mode
     */
    static public $debug = false;

         
    /**
     * Return object instance
     * @static
     * @return object instance
     */
    static public function &getInstance($privilages)
    {
        if(self::$instance === null)
        {            
            self::$instance = new Config();                        
        }        
        self::setPrivilages($privilages);
        return self::$instance;
    }
    
    /**
     * Reloading config data and creating new object instance
     */
    static public function Renew($privilages)
    {
        \Api\Session::SessionRenew();
        self::getInstance($privilages);
                
        
        $vars = array_filter(get_class_vars('\Api\Config'),function($var)
        {
           return  $var != 'instance';
        });
        
        if(array_key_exists($privilages.self::_CONFIG_, $_SESSION))
        {
            
            foreach($vars as $k => $v)
            {                            
                if(array_key_exists($k, $_SESSION[$privilages.self::_CONFIG_]))
                  self::${$k} = &$_SESSION[$privilages.self::_CONFIG_][$k];
            }    
            
            self::$privilages = $privilages;
        }
         
    } 
    
    /**
     * Unsetting all 
     */
    static public function UnsetConfig($privilages)
    {
        \Api\Session::SessionRenew();
        if(!array_key_exists($privilages.self::_CONFIG_, $_SESSION)) return false;
        
        $vars = array_filter(get_class_vars('\Api\Config'),function($var)
        {
           return  $var != 'instance';
        });
        
        foreach($vars as $k => $v)
        {                           
            if(array_key_exists($k, $_SESSION[$privilages.self::_CONFIG_]))
                $_SESSION[$privilages.self::_CONFIG_][$k]=null;
        }
        
        return true;
    }
    
    /**
     * Allow to storing configure data beatwen session
     */
    static public function set($name, &$val)
    {                
        \Api\Session::SessionRenew();
        
        $_SESSION[self::$privilages.self::_CONFIG_][$name]= &$val;        
        self::${$name} = &$val;
    }
    
    /**
     * Set config privilages
     */
    static public function setPrivilages($privilages)
    {
        self::$privilages = $privilages;
        
    }
    
    /**
     * 
     */
    private function __construct(){        
    }
    
    /**
     * Merege two configure arrays into one.
     * If $overwrite is set true, then method
     * overwrite all comparing columns
     * @static 
     * @author Lukasz Torba
     * @param array of config arrays, bool overwrite state
     * @return config array
     */
     static public function Merge(array $_configs, $overwrite=true)
     {
        $out = array();
        
        foreach($_configs as $k => $v)
        {
            foreach($v as $l => $m)
            {
                if(!array_key_exists($l, $out) || $overwrite)
                    $out[$l]=$m;
            }
        }
        
        return $out;
     }
     
     /**
      * Loading config file into array. 
      * @static 
      * @author Łukasz Torba
      * @param path to config file, default loading global config
      * @return config array or throw
      */
      static public function Load($_module = null, $loadGlobal = true)
      {
        $globalConfig = array();
        if(file_exists(BASE_PATH.'/config/global.php') &&  $loadGlobal)
        {
            $globalConfig = include BASE_PATH.'/config/global.php';        
            if($_module == null) return $globalConfig;
        }
        
        $moduleConfig = array();            
        if($_module != null && file_exists(BASE_PATH.'/'.$_module.'/config.php'))			
            $moduleConfig = include BASE_PATH.'/'.$_module.'/config.php'; // include module config  
            
        return self::Merge(array($globalConfig, $moduleConfig), true);          
      }
            
 }
}?>